/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * HelpWindow.java
 *
 * Created on 5/11/2009, 08:50:16 AM
 * http://www.devshed.com/c/a/Java/Java-Help-Files/3/
 */

package pradoeditor.comun;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.net.URL;
import javax.swing.JFrame;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import pradoeditor.comun.logger.CustomLogger;
import pradoeditor.comun.logger.LoggerHelper;

/**
 *
 * @author jaimeg
 */
public class HelpWindow extends JFrame implements ActionListener
{

  private final int WIDTH = 600;
  private final int HEIGHT = 400;
  private URL helpURL;

    /** Creates new form HelpWindow */
    public HelpWindow(String title, URL hlpURL)
    {
        super(title);
        initComponents();

        this.helpURL = hlpURL;

        try
        {
          editorpane.setPage(helpURL);
          setVisible(true);
        }
        catch (Exception ex)
        {
          LoggerHelper.getLogger(this).error(ex);
        }
      //anonymous inner listener
      editorpane.addHyperlinkListener(new HyperlinkListener()
      {
          public void hyperlinkUpdate(HyperlinkEvent ev)
          {
              try
              {
                  if (ev.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
                  {
                      editorpane.setPage(ev.getURL());
                  }
              }
              catch (IOException ex) {
                LoggerHelper.getLogger(this).error(ex);
              }
          }
      });

        calculateLocation();
    }

public void actionPerformed(ActionEvent e)
{
    String strAction = e.getActionCommand();
    URL tempURL;
    try
    {
        if (strAction.equals("Contents"))
        {
            tempURL = editorpane.getPage();
            editorpane.setPage(helpURL);
        }
        if (strAction.equals("Close"))
        {
            // more portable if delegated
            processWindowEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
        }
    }
    catch (IOException ex)
    {
        CustomLogger.writeError(this, ex);
    }
}

private void calculateLocation()
{
    Dimension screendim = Toolkit.getDefaultToolkit().getScreenSize();
    setSize(new Dimension(WIDTH, HEIGHT));
    int locationx = (screendim.width - WIDTH) / 2;
    int locationy = (screendim.height - HEIGHT) / 2;
    setLocation(locationx, locationy);
}
 
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        editorpane = new javax.swing.JEditorPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        editorpane.setEditable(false);
        jScrollPane1.setViewportView(editorpane);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JEditorPane editorpane;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

}
